package com.aistar.interceptor;

import com.aistar.model.entity.Employee;
import com.aistar.model.entity.Employer;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: LoginInterceptor
 * @date: 2021/10/18 21:10
 * @Description: TODO...本类用来演示:拦截器
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // ToDo...Session存储信息时的拦截方式
        HttpSession session = request.getSession();
        Employer employer = (Employer) session.getAttribute("employer");
        Employee employee = (Employee) session.getAttribute("employee");
        if(null==employer && null==employee){
            // 重定向到登录页面
            response.sendRedirect("/hire/login");
            return false;
        }
        return true;
    }
}
